package pl.dream.team.fuel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;

import pl.dream.team.dao.intf.InscriptionDao;
import pl.dream.team.domain.Inscription;

@ViewScoped
@ManagedBean(name = "inscriptionBean")
public class InscriptionViewBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8151792734271624978L;

	@Inject
	private InscriptionDao inscriptionDao;

	private List<Inscription> inscriptionsList;

	private Inscription inscription;


	@PostConstruct
	private void init() {
		inscription = new Inscription();
		inscriptionsList = inscriptionDao.findInscriptions();
	}

	public List<Inscription> getInscriptionsList() {
		return inscriptionsList;
	}

	public void setInscriptionsList(List<Inscription> inscriptionsList) {
		this.inscriptionsList = inscriptionsList;
	}

	public String saveData() {
		inscription.setInsertiondate(new Date());
		inscription
				.setAverageFuelConsumption(calculateAverageFuelConsumption());
		inscription.setFuelPrice(calculateFuelPrice());
		inscriptionDao.addInscription(inscription);
		return "index.xhtml?faces-redirect=true";
	}

	private Double calculateAverageFuelConsumption() {
		return (inscription.getAmount() / inscription.getMeterState()) * 100;
	}

	private Double calculateFuelPrice() {
		return inscription.getExpenses() / inscription.getAmount();
	}

	public Inscription getInscription() {
		return inscription;
	}

	public void setInscription(Inscription inscription) {
		this.inscription = inscription;
	}

}
